﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mbs.ComponentModel;
using NUnit.Framework;

namespace Mbs.Test.Undo
{
    [TestFixture]
    public class UndoActionTest4
    {
        class Employee : UndoableObject
        {
            private string name;
            private int age;

            public string Name
            {
                get { return name; }
                set
                {
                    if (!string.Equals(value, name))
                    {
                        var old = name;
                        name = value;
                        OnPropertyChanged("Name", value, old);
                    }
                }
            }

            public int Age
            {
                get { return age; }
                set
                {
                    if (value != age)
                    {
                        var old = age;
                        age = value;
                        OnPropertyChanged("Age", value, old);
                    }
                }
            }
        }

        [Test]
        public void Test()
        {
            UndoManager undoMgr = new UndoManager();
            UndoManager.Current = undoMgr;

            Employee emp = new Employee();
            

            Assert.IsNullOrEmpty(emp.Name);
            emp.Name = "ZhangSan";

            Assert.IsTrue(undoMgr.CanUndo);
            undoMgr.Undo();
            Assert.IsNullOrEmpty(emp.Name);

            undoMgr.Redo();
            Assert.IsTrue(string.Equals("ZhangSan", emp.Name));

            undoMgr.Clear();
            emp.BeginEdit();
            emp.Age = 18;
            Assert.IsTrue(emp.IsChanged);
            Assert.IsTrue(emp.IsEdit);
            Assert.IsFalse(undoMgr.CanUndo);
            emp.EndEdit();

            Assert.IsTrue(emp.IsChanged);
            Assert.IsFalse(emp.IsEdit);


            emp.AcceptChanges();
            Assert.IsFalse(emp.IsChanged);
        }
    }
}
